package com.caipin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caipin.dao.TeacherDao;
import com.caipin.dao.TeacherWorkDao;
import com.caipin.domain.table.Teacher;
import com.caipin.domain.table.TeacherWork;
import com.caipin.domain.web.PageBean;
import com.caipin.service.TeacherManagementService;
import com.caipin.service.TeacherService;
import com.caipin.service.TeacherWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: zhunian0322@163.com
 * @DateTime: 2020-08-01 9:12
 * @Description: TODO
 */
@Service
public class TeacherManagementServiceImpl implements TeacherManagementService {

    @Autowired
    private TeacherDao teacherDao;

    @Autowired
    private TeacherService teacherIService;

    @Autowired
    private TeacherWorkDao teacherWorkDao;

    @Autowired
    private TeacherWorkService teacherWorkIService;

    @Autowired
    private TeacherPhotoDao teacherPhotoDao;

    @Autowired
    private FaceImageDataDao faceImageDataDao;

    public int importTeacherToMysql(List<Teacher> teacherList) {
        this.teacherDao.truncateData();
        boolean b = this.teacherIService.saveBatch(teacherList);
        if (!b)
            return 0;
        return teacherList.size();
    }

    public PageBean<Teacher> findTeacherList(Integer pageNum, Integer pageSize) {
        Page<Teacher> teacherPage = new Page(pageNum.intValue(), pageSize.intValue());
        IPage<Teacher> page = this.teacherDao.selectPage((IPage)teacherPage, null);
        PageBean<Teacher> pageBean = new PageBean();
        pageBean.setCurrPage(Long.valueOf(page.getCurrent()));
        pageBean.setPageSize(Integer.valueOf(page.getRecords().size()));
        pageBean.setTotalPage(Long.valueOf(page.getPages()));
        pageBean.setTotalCount(Long.valueOf(page.getTotal()));
        pageBean.setList(page.getRecords());
        return pageBean;
    }

    @Transactional
    public void delTeacher(String id) {
        QueryWrapper<TeacherPhoto> queryWrapper = new QueryWrapper();
        queryWrapper.eq("teacher_id", id);
        this.teacherPhotoDao.delete((Wrapper)queryWrapper);
        this.teacherDao.deleteById(id);
        QueryWrapper<FaceImageData> faceImageDataQueryWrapper = new QueryWrapper();
        ((QueryWrapper)((QueryWrapper)faceImageDataQueryWrapper
                .eq("person_id", id))
                .orderByDesc("create_timestamp"))
                .last("LIMIT 1");
        FaceImageData faceImageData = (FaceImageData)this.faceImageDataDao.selectOne((Wrapper)faceImageDataQueryWrapper);
        if (null == faceImageData)
            return;
        faceImageData.setAction("delete");
        faceImageData.setTimestamp(Long.valueOf(System.currentTimeMillis() / 1000L));
        this.faceImageDataDao.updateById(faceImageData);
    }

    public Teacher findTeacherById(String id) {
        return (Teacher)this.teacherDao.selectById(id);
    }

    public void addTeacher(Teacher teacher) {
        this.teacherDao.insert(teacher);
    }

    public void updateTeacher(Teacher teacher) {
        this.teacherDao.updateById(teacher);
    }

    public int importTeacherWorkToMysql(List<TeacherWork> teacherWorkList) {
        boolean b = this.teacherWorkIService.saveBatch(teacherWorkList);
        if (!b)
            return 0;
        return teacherWorkList.size();
    }

    public PageBean<TeacherWork> findTeacherWorkList(Integer pageNum, Integer pageSize, String teacherId) {
        Page<TeacherWork> teacherWorkPage = new Page(pageNum.intValue(), pageSize.intValue());
        QueryWrapper<TeacherWork> queryWrapper = new QueryWrapper();
        queryWrapper.eq("teacher_id", teacherId);
        IPage<TeacherWork> page = this.teacherWorkDao.selectPage((IPage)teacherWorkPage, (Wrapper)queryWrapper);
        PageBean<TeacherWork> pageBean = new PageBean();
        pageBean.setCurrPage(Long.valueOf(page.getCurrent()));
        pageBean.setPageSize(Integer.valueOf(page.getRecords().size()));
        pageBean.setTotalPage(Long.valueOf(page.getPages()));
        pageBean.setTotalCount(Long.valueOf(page.getTotal()));
        pageBean.setList(page.getRecords());
        return pageBean;
    }

    public PageBean<TeacherWork> findTeacherWorkList(Integer pageNum, Integer pageSize) {
        Page<TeacherWork> teacherWorkPage = new Page(pageNum.intValue(), pageSize.intValue());
        IPage<TeacherWork> page = this.teacherWorkDao.selectPage((IPage)teacherWorkPage, null);
        PageBean<TeacherWork> pageBean = new PageBean();
        pageBean.setCurrPage(Long.valueOf(page.getCurrent()));
        pageBean.setPageSize(Integer.valueOf(page.getRecords().size()));
        pageBean.setTotalPage(Long.valueOf(page.getPages()));
        pageBean.setTotalCount(Long.valueOf(page.getTotal()));
        pageBean.setList(page.getRecords());
        return pageBean;
    }

    public PageBean<TeacherWork> findTeacherWorkList(Integer pageNum, Integer pageSize, String teacherName, String courseName, String grade, String clbum, Integer week, Integer sections, String classroom) {
        Page<TeacherWork> teacherWorkPage = new Page(pageNum.intValue(), pageSize.intValue());
        QueryWrapper<TeacherWork> queryWrapper = new QueryWrapper();
        if (teacherName != null)
            queryWrapper.eq("teacher_name", teacherName);
        if (courseName != null)
            queryWrapper.eq("course_name", courseName);
        if (grade != null)
            queryWrapper.eq("grade", grade);
        if (clbum != null)
            queryWrapper.eq("clbum", clbum);
        if (week != null)
            queryWrapper.eq("week", week);
        if (sections != null)
            queryWrapper.eq("sections", sections);
        if (classroom != null)
            queryWrapper.eq("classroom", classroom);
        IPage<TeacherWork> page = this.teacherWorkDao.selectPage((IPage)teacherWorkPage, (Wrapper)queryWrapper);
        PageBean<TeacherWork> pageBean = new PageBean();
        pageBean.setCurrPage(Long.valueOf(page.getCurrent()));
        pageBean.setPageSize(Integer.valueOf(page.getRecords().size()));
        pageBean.setTotalPage(Long.valueOf(page.getPages()));
        pageBean.setTotalCount(Long.valueOf(page.getTotal()));
        pageBean.setList(page.getRecords());
        return pageBean;
    }

    public void delTeacherWorkById(Long id) {
        this.teacherWorkDao.deleteById(id);
    }

    public int delTeacherWorkListByIds(List<Long> ids) {
        return this.teacherWorkDao.deleteBatchIds(ids);
    }

    public TeacherWork findTeacherWorkById(long id) {
        return (TeacherWork)this.teacherWorkDao.selectById(Long.valueOf(id));
    }

    public void updateTeacherWork(TeacherWork teacherWork) {
        this.teacherWorkDao.updateById(teacherWork);
    }

    public void addTeacherWork(TeacherWork teacherWork) {
        this.teacherWorkDao.insert(teacherWork);
    }

    public List<TeacherWork> findTeacherWorkByCourse(String courseName, String grade, Integer week, Integer sections, String classroom) {
        QueryWrapper<TeacherWork> queryWrapper = new QueryWrapper();
        ((QueryWrapper)((QueryWrapper)((QueryWrapper)((QueryWrapper)queryWrapper.eq("course_name", courseName))
                .eq("grade", grade))
                .eq("week", week))
                .eq("sections", sections))
                .eq("classroom", classroom);
        List<TeacherWork> teacherWorkList = this.teacherWorkDao.selectList((Wrapper)queryWrapper);
        return teacherWorkList;
    }

    public void cleanTeacherWork() {
        this.teacherWorkDao.truncateData();
    }

    public List<TeacherWork> findTeacherWorkListByCourse(String courseName, String grade) {
        QueryWrapper<TeacherWork> queryWrapper = new QueryWrapper();
        ((QueryWrapper)queryWrapper.eq("course_name", courseName))
                .eq("grade", grade);
        List<TeacherWork> teacherWorkList = this.teacherWorkDao.selectList((Wrapper)queryWrapper);
        return teacherWorkList;
    }

    @Transactional
    public void saveTeacherPhoto(Teacher teacher, String imagePath, String imageBase64) {
        this.teacherPhotoDao.delete((Wrapper)(new QueryWrapper()).eq("teacher_id", teacher.getId()));
        TeacherPhoto teacherPhoto = new TeacherPhoto();
        teacherPhoto.setTeacherId(teacher.getId());
        teacherPhoto.setTeacherImageBase64(imageBase64);
        this.teacherPhotoDao.insert(teacherPhoto);
        QueryWrapper<FaceImageData> queryWrapper = new QueryWrapper();
        ((QueryWrapper)((QueryWrapper)queryWrapper
                .eq("person_id", teacher.getId()))
                .orderByDesc("create_timestamp"))
                .last("LIMIT 1");
        FaceImageData faceImageData = (FaceImageData)this.faceImageDataDao.selectOne((Wrapper)queryWrapper);
        if (null == faceImageData)
            faceImageData = new FaceImageData();
        faceImageData.setAction("update");
        faceImageData.setPersonName(teacher.getName());
        faceImageData.setPersonId(teacher.getId());
        faceImageData.setTimestamp(Long.valueOf(System.currentTimeMillis() / 1000L));
        faceImageData.setImagePath(imagePath);
        faceImageData.setPersonType("teacher");
        faceImageData.setImageBase64(imageBase64);
        if (null == faceImageData.getId()) {
            this.faceImageDataDao.insert(faceImageData);
        } else {
            this.faceImageDataDao.updateById(faceImageData);
        }
    }
}
